<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/3/2
 * Time: 17:25
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Beautydiaryreply extends Base
{
    /**
     * 回复列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(Request::instance()->isAjax()){
            $start_time = input('start_time');
            $end_time = input('end_time');
            $reply_from = input('reply_from');
            $reply_user_name = input('reply_user_name');
            $reply_user_phone = input('reply_user_phone');
            $reply_content = input('reply_content');
            $where['reply_status'] = 1;
            // 时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['reply_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['reply_time'] = ['> time', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['reply_time'] = ['< time', $end_time];
            }
            // 来源
            if(isset($reply_from) && $reply_from != ''){
                $where['reply_from'] = $reply_from;
            }
            // 姓名
            if(isset($reply_user_name) && $reply_user_name != ''){
                $where['user_username|username'] = ['like', "%$reply_user_name%"];
            }
            // 手机号
            if(isset($reply_user_phone) && $reply_user_phone != ''){
                $where['user_phone|phone'] = ['like', "%$reply_user_phone%"];
            }
            // 内容
            if(isset($reply_content) && $reply_content != ''){
                $where['reply_content'] = ['like', "%$reply_content%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('beauty_diary_reply')
                ->join('up_u_user', 'reply_user_id = user_id', 'LEFT')
                ->join('up_admin', 'id = reply_admin_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('beauty_diary_reply')
                ->join('up_u_user', 'reply_user_id = user_id', 'LEFT')
                ->join('up_admin', 'id = reply_admin_id', 'LEFT')
                ->where($where)
                ->field('reply_id, reply_type, reply_content, reply_from, reply_time, user_username, username, 
                user_phone, phone, reply_comment_id')
                ->order('reply_time desc')
                ->page($Nowpage,$limits)
                ->select();
            if($lists){
                foreach($lists as $k => $v){
                    // 发布者&手机号
                    if($v['reply_from'] == 1){
                        $lists[$k]['reply_user'] = $v['user_username'];
                        $lists[$k]['reply_phone'] = $v['user_phone'];
                    }else{
                        $lists[$k]['reply_user'] = $v['username'];
                        $lists[$k]['reply_phone'] = $v['phone'];
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 回复回复
     * @return mixed|\think\response\Json
     */
    public function replyReply(){
        $reply_id = input('reply_id');
        if(Request::instance()->isPost()){
            $reply_content = input('reply_content');
            $reply = Db::table('up_beauty_diary_reply')
                ->where('reply_id', $reply_id)
                ->field('reply_bd_id, reply_comment_id')
                ->find();
            $insert = [
                'reply_type' => 2,
                'reply_user_id' => 0,
                'reply_admin_id' => session('uid'),
                'reply_bd_id' => $reply['reply_bd_id'],
                'reply_comment_id' => $reply['reply_comment_id'],
                'reply_reply_id' => $reply_id,
                'reply_content' => $reply_content,
                'reply_from' => 2,
                'reply_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_beauty_diary_reply')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '回复成功']);
            }else{
                return json(['code' => 100, 'msg' => '回复失败']);
            }
        }
        return $this->fetch('beautydiaryreply/replyreply', ['reply_id' => $reply_id]);
    }
    
    /**
     * 删除回复
     * @return \think\response\Json
     */
    public function replyDel(){
        $reply_id = input('id');
        $res = Db::table('up_beauty_diary_reply')->where('reply_id', $reply_id)->setField('reply_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 删除回复并冻结用户
     * @return \think\response\Json
     */
    public function replyDelUser(){
        $reply_id = input('id');
        $reply = Db::table('up_beauty_diary_reply')->where('reply_id', $reply_id)->field('reply_user_id')->find();
        Db::startTrans();
        try{
            // 冻结用户
            Db::table('up_u_user')->where('user_id', $reply['reply_user_id'])->setField('user_status', 2);
            // 删除回复
            Db::table('up_beauty_diary_reply')->where('reply_id', $reply_id)->setField('reply_status', 2);
            
            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch(\Exception $e){
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }
}